﻿using Prism.Commands;
using Prism.Events;
using Prism.Modularity;
using Prism.Regions;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UI.Application.Share.Event;
using UI.Application.Share.Model;
using UI.Application.Share.Names;

namespace Shell.ViewModels
{
    internal class MainWindowViewModel:ReactiveObject
    {
        private IRegionManager RegionManager { get; }
        private IModuleManager ModuleManager { get; }
        private IEventAggregator EventAggregator { get; }

        public bool IsHardwareInitialized {  get; private set; }=false;
        public ICommand LoadedCommand { get; }
        public MainWindowViewModel(IRegionManager regionManager,IModuleManager moduleManager,IEventAggregator eventAggregator)
        {
            EventAggregator=eventAggregator;
            RegionManager = regionManager;
            ModuleManager = moduleManager;
            LoadedCommand = new DelegateCommand(LoadWindow);
        }

        private void LoadWindow()
        {
            //ModuleManager.LoadModule("ApplicationLoginModule");//这里有一个延时加载的概念。
            RegionManager.RequestNavigate(RegionNames.MainRegion,ViewNames.LoginView);
            EventAggregator.GetEvent<LoginSuccessEvent>().Subscribe(LoginSuccess,ThreadOption.UIThread);
            EventAggregator.GetEvent<InitialiezdSuccessEvent>().Subscribe(()=>this.IsHardwareInitialized=true);//用户登入成功后这里修改胃true
        }

        private void LoginSuccess(CurrentUser user)
        {
            if (IsHardwareInitialized) 
            {
                //加载主界面
                ModuleManager.LoadModule(ModuleNames.ApplicationMainModule);
                RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.MainView);
            }
            else
            {
                //登入成功后加载初始化模块
                ModuleManager.LoadModule( ModuleNames.AppilcationInitializeModule);
                RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.InitializeView);
            }
            
           

        }
    }
}
